<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box01{
            width: 100px;
            height: 100px;
            background: pink;
            position: absolute;
            left: 0;
            top: 0;
        }
        .box02{
            width: 100px;
            height: 100px;
            background: skyblue;
            position: absolute;
            left: 300px;
            top: 0;
        }
    </style>
</head>
<body>
    <div class="box01"></div>
    <div class="box02"></div>
</body>
</html>
<script>
    // let Box01 = document.querySelector(".box01");
    // // 鼠标按下
    // Box01.onmousedown = function(e){
    //     let x = e.clientX - this.offsetLeft;
    //     let y = e.clientY - this.offsetTop;
    //     // 鼠标离开元素
    //     this.onmousemove = function(e){
    //         let xx = e.clientX;
    //         let yy = e.clientY;
    //         this.style.left = xx - x + "px";
    //         this.style.top = yy - y + "px";
    //     }
    // }
    // // 清除事件
    // Box01.onmouseup = function(){
    //     this.onmousemove = "";
    // }

    // 封装
    class Drag{
        constructor(Box01){
            this.Box01 = Box01;
            this.dowmFn();
            this.upFn();
        }
        // 鼠标按下
        dowmFn(){
            let that = this;
            this.Box01.onmousedown = function(e){
                let x = e.clientX - this.offsetLeft;
                let y = e.clientY - this.offsetTop;
                // 调用鼠标离开元素函数
                that.moveFn(x,y);
            }
        }
        // 鼠标离开元素
        moveFn(x,y){
            this.Box01.onmousemove = function(e){
                let xx = e.clientX;
                let yy = e.clientY;
                this.style.left = xx - x + "px";
                this.style.top = yy - y + "px";
            }
        }
        // 鼠标抬起
        upFn(){
            this.Box01.onmouseup = function(){
                this.onmousemove = "";
            }
        }
    }

    // 子类 控制方块移动边界
    class SonDrag extends Drag{
        moveFn(x,y){
            this.Box01.onmousemove = function(e){
                let xx = e.clientX;
                let yy = e.clientY;
                let xdis = xx - x;
                let ydis = yy - y;
                // 左边的范围
                if(xdis<0){
                    xdis = 0;
                }
                // 右边的范围
                if(xdis>(window.innerWidth - this.offsetWidth)){
                    xdis = window.innerWidth - this.offsetWidth;
                }
                // 上边的范围
                if(ydis<0){
                    ydis = 0;
                }
                // 下边的范围
                if(ydis>(window.innerHeight - this.offsetHeight)){
                    ydis = window.innerHeight - this.offsetHeight;
                }
                this.style.left = xdis + "px";
                this.style.top = ydis + "px";
            }
        }
    }


    // 获取两个方块元素
    let Box01 = document.querySelector(".box01");
    // 第一个方块可以出视口
    let drag1 = new Drag(Box01);
    let Box02 = document.querySelector(".box02");
    // 第二个不可以出视口
    let drag2 = new SonDrag(Box02);

</script>